<?php
namespace Admin\Controller;
use Think\Controller;

class ItemrateController extends Controller {

	function __construct() {
		//继承父类
		parent::__construct();

		//判断登录状态
		if(!D('Admin')->islogin()){//未登录
			$this->error('您尚未登录，请先登录！',U('login/login'),3);
		}
		//判断操作是否授权
		if(!D('Admin')->isoperate(CONTROLLER_NAME,ACTION_NAME)){//未授权
			$this->error('您尚无此操作权限！',U('index/welcome'),3);
		}
    }

	//空方法，防止报错
	public function _empty(){
        $this->index();
    }
	
	//项目评级
    public function index(){
		if(IS_POST){
			$info=I("info");
			$info['item_addtime']=SYS_TIME;
			$item_id=M("Iteminfo")->add($info);
			if($item_id){
				redirect(U('itemrate/rateing',array("item_id"=>$item_id)),0, '页面跳转中...');
			}else{
				$this->error('参数错误，请重新来过！',U('itemrate/index'),3);
			}
		}else{
			//行业分类信息
			$indusinfo=M("Industry")->where("indus_pid=0")->select();
			foreach($indusinfo as $k=>$v){
				$child=M("Industry")->where("indus_pid=".$v['indus_id'])->select();
				$indusinfo[$k]['child']=$child;
				$indusinfo[$k]['childnum']=sizeof($child);
			}
			$this->assign('indusinfo',$indusinfo);
			//省信息
			$provinces=M("Provinces")->where("1")->select();
			$this->assign('provinces',$provinces);
			$this->display();
		}
    }
	//项目开始调查问卷
	public function rateing(){
		$item_id=I("get.item_id");
		if(IS_POST){
			$rating=I("info");
			$RatingModel = new \Home\Model\RatingModel();
			$flag=$RatingModel->arrange($item_id,$rating);
			//查看项目行业是否启用
			$iteminfo=M("Iteminfo")->join("left join ec_industry on ec_iteminfo.indus_id=ec_industry.indus_id")->where("item_id=".$item_id)->field("ec_iteminfo.*,ec_industry.indus_isopen,ec_industry.indus_score,ec_industry.indus_weight")->find();
			if($iteminfo['indus_isopen']==1){//1为启用，2为测试
				redirect(U('itemrate/rateingshow',array("item_id"=>$item_id)),0, '页面跳转中...');
			}elseif($iteminfo['indus_isopen']==2){
				$this->success('请耐心等待专家测评！',U('itemrate/index'),3);
			}
		}else{
			//项目基本信息
			$iteminfo=M("Iteminfo")->where("item_id=".$item_id)->find();
			$this->assign('iteminfo',$iteminfo);
			//评测项信息
			$RatingModel = new \Home\Model\RatingModel();
			$rateinfo=$RatingModel->rateinfo($item_id);
			$this->assign('rateinfo',$rateinfo);

			$this->display();
		}
	}
	//显示测评结果
	public function rateingshow(){
		$item_id=I("get.item_id");
		//项目基本信息
		$iteminfo=M("Iteminfo")->where("item_id=".$item_id)->find();
		$this->assign('iteminfo',$iteminfo);

		//行业分类信息
		$indusinfo=M("Industry")->where("indus_pid=0")->select();
		foreach($indusinfo as $k=>$v){
			$child=M("Industry")->where("indus_pid=".$v['indus_id'])->select();
			$indusinfo[$k]['child']=$child;
			$indusinfo[$k]['childnum']=sizeof($child);
		}
		$this->assign('indusinfo',$indusinfo);
		//省信息
		$provinces=M("Provinces")->where("1")->select();
		$this->assign('provinces',$provinces);

		$this->display();
	}

	//后台手动设置分值-并计算总分计入项目信息中
	public function handscore(){
		$item_id=I("get.item_id");
		if(IS_POST){
			$rate=I("rate");
			$RatingModel = new \Home\Model\RatingModel();
			$RatingModel->ratescore($item_id,$rate);
			redirect(U('itemrate/rateingshow',array("item_id"=>$item_id)),0, '页面跳转中...');
		}else{
			//项目基本信息
			$iteminfo=M("Iteminfo")->where("item_id=".$item_id)->find();
			$this->assign('iteminfo',$iteminfo);
			//评测项信息
			$RatingModel = new \Home\Model\RatingModel();
			$rateinfo=$RatingModel->rateinfo($item_id);
			$this->assign('rateinfo',$rateinfo);
			//各项分值信息
			$Rating=M("Rating")->where("item_id=".$item_id)->select();
			foreach($Rating as $k=>$v){
				$rate[$v['three_id']]=$v;
			}
			$this->assign('rate',$rate);
			$this->display();
		}
	}

	//根据省名称获取市信息
	public function getcity(){
		$province=I("get.province");
		$citys=M("Cities")->where("ec_provinces.province='".trim($province)."'")->join("left join ec_provinces on ec_provinces.id=ec_cities.provinceid")->field("ec_cities.*")->select();
		$this->ajaxReturn(array("data"=>$citys));
	}



	//项目手动打分（项目列表）
	public function iteminfo(){
		$where="1 and score=0 ";
		$info=I("info");
		if ($info){
			@extract($info);
			if($keyword){
				$info['keyword']=urldecode($info['keyword']);
				$where.="and (item_name like '%".urldecode($info['keyword'])."%' or item_company like '%".urldecode($info['keyword'])."%' or item_linkmen like '%".urldecode($info['keyword'])."%' or item_linkphone like '%".urldecode($info['keyword'])."%') ";$this->assign('keyword',urldecode($info['keyword']));
			}
			if($starttime){
				$where.=" and ec_iteminfo.item_addtime>=".strtotime($starttime." 00:00:00")." ";
				$this->assign('starttime',$starttime);
			}
			if($endtime){
				$where.=" and ec_iteminfo.item_addtime<=".strtotime($endtime." 23:59:59")." ";
				$this->assign('endtime',$endtime);
			}
			if($indus_id){
				$where.=" and ec_iteminfo.indus_id=".$indus_id." ";
				$this->assign('indus_id',$indus_id);
			}
			if($item_province){
				$info['item_province']=urldecode($info['item_province']);
				$where.=" and ec_iteminfo.item_province='".urldecode($info['item_province'])."' ";
				$this->assign('item_province',urldecode($info['item_province']));
			}
			if($item_city){
				$info['item_city']=urldecode($info['item_city']);
				$where.=" and ec_iteminfo.item_city='".urldecode($info['item_city'])."' ";
				$this->assign('item_city',urldecode($info['item_city']));
			}
		}

		$Iteminfo=M('Iteminfo');
		// 查询满足要求的总记录数
		$count      = $Iteminfo->where($where)->count();
		$Page       = new \Think\Page($count,15);
		//分页跳转的时候保证查询条件
		if($info){
			foreach($info as $key=>$val) {
				$Page->parameter['info['.$key.']']   =   urlencode($val);
			}
		}
		$pageshow   = $Page->adminshow();
		// 进行分页数据查询
		$listinfo = $Iteminfo->where($where)->order('item_id desc')->join("left join ec_industry on ec_industry.indus_id=ec_iteminfo.indus_id")->limit($Page->firstRow.','.$Page->listRows)->field("ec_iteminfo.*,ec_industry.indus_title")->order("ec_iteminfo.item_id desc")->select();
		$this->assign('listinfo',$listinfo);
		$this->assign('pageshow',$pageshow);

		//行业分类信息
		$indusinfo=M("Industry")->where("indus_pid=0")->select();
		foreach($indusinfo as $k=>$v){
			$child=M("Industry")->where("indus_pid=".$v['indus_id'])->select();
			$indusinfo[$k]['child']=$child;
			$indusinfo[$k]['childnum']=sizeof($child);
		}
		$this->assign('indusinfo',$indusinfo);
		//省信息
		$provinces=M("Provinces")->where("1")->select();
		$this->assign('provinces',$provinces);

		$this->display();
	}
	//删除项目
	public function delitem(){
		$item_id=I("get.item_id");
		//删除项目表
		M("Iteminfo")->where("item_id=".$item_id)->delete();
		//删除项目评测各个项值以及分值
		M("Rating")->where("item_id=".$item_id)->delete();
		$this->success('删除成功',U('itemrate/iteminfo'),3);
	}
}
